package com.song.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.common.base.UserContextInfo;
import com.song.common.response.PageInfo;
import com.song.framework.context.ContextUserInfoThreadHolder;
import com.song.framework.exception.ServiceException;
import com.song.module.admin.request.AppChefRequest;
import com.song.module.system.entity.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.song.module.admin.request.ApChefRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.song.module.admin.mapper.ApChefMapper;
import org.springframework.beans.BeanUtils;
import com.song.module.admin.entity.ApChef;
import com.song.module.admin.service.IApChefService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 厨师业务层处理
 *
 * @author song
 * @date 2024-03-06
 */
@Slf4j
@Service
@AllArgsConstructor
public class ApChefServiceImpl implements IApChefService {

    private final ApChefMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApChef add(ApChefRequest request) {
        ApChef entity = ApChef.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ApChefRequest request) {
        ApChef entity = ApChef.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public ApChef getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<ApChef> pageList(ApChefRequest request) {
        Page<ApChef> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<ApChef> query = this.buildWrapper(request);
        Page<ApChef> chefPage = baseMapper.selectPage(page, query);
        List<ApChef> records = chefPage.getRecords();
        health_card_replace(records);

        chefPage.setRecords(records);
        return PageInfo.build(chefPage);
    }

    @Override
    public void verify(AppChefRequest request) {
        ApChef apChef = baseMapper.selectOne(Wrappers.lambdaQuery(ApChef.class)
                .eq(ApChef::getName, request.getName())
                .eq(ApChef::getIdCard, request.getIdCard()).eq(ApChef::getMobile, request.getMobile()));
        if (Objects.nonNull(apChef)) {
            throw new ServiceException("当前已提交审核");
        }
        UserContextInfo info = ContextUserInfoThreadHolder.get();
        ApChef entity = ApChef.builder().build();
        BeanUtils.copyProperties(request, entity);
        entity.setAvatar(info.getAvatar());
        baseMapper.insert(entity);
    }

    @Override
    public List<ApChef> list(ApChefRequest request) {
        LambdaQueryWrapper<ApChef> query = this.buildWrapper(request);

        List<ApChef> apChefs = baseMapper.selectList(query);

        health_card_replace(apChefs);
        return apChefs;
    }

    private void health_card_replace(List<ApChef> apChefs) {
        apChefs.stream().map(apChef -> {
            String healthCard = apChef.getHealthCard();
            if (healthCard.contains("[") || healthCard.contains("]")){
                String replace = healthCard.replace("[", "").replace("]", "");
                apChef.setHealthCard(replace);
            }
            return apChef;
        });
    }



    private LambdaQueryWrapper<ApChef> buildWrapper(ApChefRequest param) {
        LambdaQueryWrapper<ApChef> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(ApChef::getName, param.getName());
        }
        if (StringUtils.isNotBlank(param.getAvatar())) {
            query.like(ApChef::getAvatar, param.getAvatar());
        }
        if (param.getStatus() != null) {
            query.eq(ApChef::getStatus, param.getStatus());
        }
        query.orderByDesc(BaseEntity::getCreateTime);
        return query;
    }

}
